home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 September / macformat-004.iso / Shareware City / Games / Jotto ][ 1.1.source Folder / Jotto ][ ƒ / Shell ƒ / graphics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-06  |  16.4 KB  |  477 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        graphics.c
  4.  
  5. Purpose:    This module handles opening/closing/updating all windows:
  6.             this includes manipulating offscreen GWorlds & bitmaps
  7.             for fun and profit.
  8.  
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program in a file named "GNU General Public License".
  21. If not, write to the Free Software Foundation, 675 Mass Ave,
  22. Cambridge, MA 02139, USA.
  23.  
  24. \**********************************************************************/
  25.  
  26. #include "graphics.h"
  27. #include "about.h"
  28. #include "about MSG.h"
  29. #include "help.h"
  30. #include "dialogs.h"
  31. #include "error.h"
  32. #include "menus.h"
  33. #include "environment.h"
  34. #include "prefs.h"
  35. #include "program globals.h"
  36.  
  37. WindowPtr        gTheWindow[NUM_WINDOWS];            /* windowptr of nth window */
  38. ExtendedWindowDataHandle
  39.                 gTheWindowData[NUM_WINDOWS];        /* handle to nth window data struct */
  40.  
  41. /* internal global variables for use by graphics.c only */
  42. static    Rect        gBoundsRect[NUM_WINDOWS];        /* rectangle of offscreen bitmap */
  43. static    Rect        gMainScreenBounds;                /* bounds of main monitor */
  44. static    GWorldPtr    gTheGWorld[NUM_WINDOWS];        /* offscreen graphics world */
  45. static    Ptr            gBWBitMap[NUM_WINDOWS];            /* offscreen bitmap for B/W machines */
  46. static    GrafPort    gBWGrafPort[NUM_WINDOWS];        /* offscreen grafport "  "     "     */
  47. static    GrafPtr        gBWGrafPtr[NUM_WINDOWS];        /* offscreen grafptr  "  "     "     */
  48. static    GWorldPtr    currentGWorld;
  49. static    GDHandle    currentGDHandle;
  50.  
  51. Boolean InitTheGraphics(void)
  52. {
  53.     short            i,j;
  54.     
  55.     GetMainScreenBounds();
  56.     
  57.     for (i=0; i<NUM_WINDOWS; i++)
  58.     {
  59.         /* nothing is inited; if there's an error later on, we'll know how much to */
  60.         /* clean up in ShutDownTheGraphics() */
  61.         gTheWindow[i]=(WindowPtr)0L;
  62.         gTheGWorld[i]=(GWorldPtr)0L;
  63.         gTheWindowData[i]=(ExtendedWindowDataHandle)0L;
  64.     }
  65.     
  66.     for (i=0; i<NUM_WINDOWS; i++)
  67.     {
  68.         gTheWindowData[i]=(ExtendedWindowDataHandle)NewHandle(sizeof(ExtendedWindowDataRec));
  69.         if (gTheWindowData[i]==0L)                            /* return if error */
  70.             return FALSE;
  71.         
  72.         (**(gTheWindowData[i])).offscreenNeedsUpdate=TRUE;    /* offscreen not inited */
  73.         (**(gTheWindowData[i])).windowIndex=i;                /* so we can retrieve it O(1) */
  74.         (**(gTheWindowData[i])).windowDepth=
  75.             (**(gTheWindowData[i])).maxDepth=1;                /* init at B/W */
  76.         (**(gTheWindowData[i])).isColor=FALSE;                /* init to grayscale */
  77.         for (j=0; j<MAX_TE_HANDLES; j++)
  78.             (**(gTheWindowData[i])).hTE[j]=0L;
  79.         HLockHi((Handle)gTheWindowData[i]);
  80.     }
  81.     
  82.     /* set window dispatch routines */
  83.     (**(gTheWindowData[kAbout])).dispatchProc=(dispatchProcPtr)AboutBoxDispatch;        /* see about.c */
  84.     (**(gTheWindowData[kHelp])).dispatchProc=(dispatchProcPtr)HelpWindowDispatch;    /* see help.c */
  85.     (**(gTheWindowData[kAboutMSG])).dispatchProc=(dispatchProcPtr)AboutMSGBoxDispatch;/* see about MSG.c */
  86.     
  87.     /* call window dispatch routines with "startup" message */
  88.     ((**(gTheWindowData[kAbout])).dispatchProc)((WindowDataHandle)gTheWindowData[kAbout], kStartup, 0L);
  89.     ((**(gTheWindowData[kHelp])).dispatchProc)((WindowDataHandle)gTheWindowData[kHelp], kStartup, 0L);
  90.     ((**(gTheWindowData[kAboutMSG])).dispatchProc)((WindowDataHandle)gTheWindowData[kAboutMSG], kStartup, 0L);
  91.     
  92.     return TRUE;
  93. }
  94.  
  95. void OpenTheWindow(short index)
  96. {
  97.     if (!gTheWindow[index])        /* if window exists, we'll just update it (see below) */
  98.     {
  99.         if (((**(gTheWindowData[index])).dispatchProc)    /* call window's dispatch to */
  100.                 ((WindowDataHandle)gTheWindowData[index], kInitialize, 0L)==kFailure)    /* initialize it */
  101.         {        /* default is to center window on main screen */
  102.             (**(gTheWindowData[index])).initialTopLeft.h =
  103.                 gMainScreenBounds.left + (((gMainScreenBounds.right -
  104.                 gMainScreenBounds.left) - (**(gTheWindowData[index])).windowWidth) / 2);
  105.             (**(gTheWindowData[index])).initialTopLeft.v =
  106.                 gMainScreenBounds.top + (((gMainScreenBounds.bottom -
  107.                 gMainScreenBounds.top) - (**(gTheWindowData[index])).windowHeight) / 2);
  108.         }
  109.         
  110.         (**(gTheWindowData[index])).windowBounds.left=
  111.             (**(gTheWindowData[index])).initialTopLeft.h;
  112.         
  113.         (**(gTheWindowData[index])).windowBounds.top=
  114.             (**(gTheWindowData[index])).initialTopLeft.v;
  115.             
  116.         if (((**(gTheWindowData[index])).windowType==noGrowDocProc) ||
  117.             ((**(gTheWindowData[index])).windowType==documentProc) ||
  118.             ((**(gTheWindowData[index])).windowType==movableDBoxProc) ||
  119.             ((**(gTheWindowData[index])).windowType==zoomDocProc) ||
  120.             ((**(gTheWindowData[index])).windowType==zoomNoGrow) ||
  121.             ((**(gTheWindowData[index])).windowType==rDocProc))
  122.                 (**(gTheWindowData[index])).windowBounds.top += 9;    /* compensate for title */
  123.         
  124.         /* don't put window over menu bar */
  125.         if ((**(gTheWindowData[index])).windowBounds.top < GetMBarHeight()+1)
  126.             (**(gTheWindowData[index])).windowBounds.top = GetMBarHeight()+1;
  127.         
  128.         (**(gTheWindowData[index])).windowBounds.bottom =
  129.             (**(gTheWindowData[index])).windowBounds.top +
  130.             (**(gTheWindowData[index])).windowHeight;
  131.         
  132.         (**(gTheWindowData[index])).windowBounds.right =
  133.             (**(gTheWindowData[index])).windowBounds.left +
  134.             (**(gTheWindowData[index])).windowWidth;
  135.         
  136.         KillOffscreen(index);        /* kill offscreen bitmaps that are left over */
  137.         
  138.         if (gHasColorQD)
  139.         {
  140.             /* create the color window with our specs, see IM Essentials 4-79ff */
  141.             gTheWindow[index] = NewCWindow(0L, &((**(gTheWindowData[index])).windowBounds),
  142.                 (**(gTheWindowData[index])).windowTitle, FALSE,
  143.                 (**(gTheWindowData[index])).windowType, (WindowPtr)-1L,
  144.                 (**(gTheWindowData[index])).hasCloseBox,
  145.                 (unsigned long)gTheWindowData[index]);
  146.         }
  147.         else
  148.         {
  149.             /* create the B/W window with our specs, see IM Essentials 4-82ff */
  150.             gTheWindow[index] = NewWindow(0L, &((**(gTheWindowData[index])).windowBounds),
  151.                 (**(gTheWindowData[index])).windowTitle, FALSE,
  152.                 (**(gTheWindowData[index])).windowType, (WindowPtr)-1L,
  153.                 (**(gTheWindowData[index])).hasCloseBox,
  154.                 (unsigned long)gTheWindowData[index]);
  155.         }
  156.     }
  157.     
  158.     if (gTheWindow[index])
  159.     {
  160.         ShowWindow(gTheWindow[index]);            /* immediately show this new window */
  161.         SelectWindow(gTheWindow[index]);        /* immediately select this new window */
  162.         SetPort(gTheWindow[index]);                /* important! for TE info to stick*/
  163.         /* call window's dispatch routine to alert it that it's open now */
  164.         ((**(gTheWindowData[index])).dispatchProc)((WindowDataHandle)gTheWindowData[index], kOpen, 0L);
  165.         SetPort(gTheWindow[index]);                /* important! */
  166.         UpdateTheWindow((ExtendedWindowDataHandle)gTheWindowData[index]);    /* immediately update this new window */
  167.     }
  168.     else HandleError(kNoMemory, FALSE);            /* if unsuccessful, display error */
  169. }
  170.  
  171. void GetMainScreenBounds(void)
  172. {
  173.     gMainScreenBounds = screenBits.bounds;        /* low-mem global */
  174.     gMainScreenBounds.top += GetMBarHeight();    /* don't include menu bar */
  175. }
  176.  
  177. short GetBiggestDeviceDepth(WindowDataHandle theData)
  178. {
  179.     short            index;
  180.     Rect            tempRect;
  181.     long            biggestSize;
  182.     long            tempSize;
  183.     GDHandle        thisHandle, gBiggestDevice;
  184.     
  185.     if (!gHasColorQD)
  186.         return 1;
  187.     
  188.     index=(**theData).windowIndex;
  189.     
  190.     if (!gTheWindow[index])
  191.         return (**(**GetMainDevice()).gdPMap).pixelSize;
  192.     
  193.     thisHandle = GetDeviceList();
  194.     gBiggestDevice = 0L;
  195.     biggestSize = 0L;
  196.     
  197.     while (thisHandle)
  198.     {
  199.         if (TestDeviceAttribute(thisHandle, screenDevice) &&
  200.             TestDeviceAttribute(thisHandle, screenActive))
  201.         {
  202.             if (SectRect(&(gTheWindow[index]->portRect), &((**thisHandle).gdRect),
  203.                     &tempRect))
  204.             {
  205.                 if (biggestSize < (tempSize = ((long)(tempRect.bottom - tempRect.top))*
  206.                     ((long)(tempRect.right - tempRect.left))))
  207.                 {
  208.                     biggestSize = tempSize;
  209.                     gBiggestDevice = thisHandle;
  210.                 }
  211.             }
  212.         }
  213.         thisHandle = GetNextDevice(thisHandle);
  214.     }
  215.     
  216.     return (gBiggestDevice) ? (**(**gBiggestDevice).gdPMap).pixelSize : 1;
  217. }
  218.  
  219. short GetWindowDepth(WindowDataHandle theData)
  220. {
  221.     short            index;
  222.     
  223.     index=(**theData).windowIndex;
  224.     /* if Color Quickdraw is not available, the depth must be 1. */
  225.     /* if Color Quickdraw is available and the window exists, return the window's
  226.        GWorld's graphics device's pixel map's pixel depth */
  227.     /* if Color Quickdraw is available and the window does not exist, return the
  228.        pixel depth of the main screen */
  229.     return (gHasColorQD) ? ((gTheWindow[index]) ?
  230.             (**(**(GetGWorldDevice(gTheGWorld[index]))).gdPMap).pixelSize :
  231.             (**(**GetMainDevice()).gdPMap).pixelSize) : 1;
  232. }
  233.  
  234. Boolean WindowIsColor(WindowDataHandle theData)
  235. {
  236.     short            index;
  237.     
  238.     index=(**theData).windowIndex;
  239.     return (gHasColorQD) ? ((GetWindowDepth(theData)>8) ? TRUE :
  240.         TestDeviceAttribute(GetGWorldDevice(gTheGWorld[index]), gdDevType)) : FALSE;
  241. }
  242.  
  243. void UpdateTheWindow(ExtendedWindowDataHandle theData)
  244. {
  245.     short            index;
  246.     long            offRowBytes, sizeOfOff;
  247.     unsigned long    updateResult;
  248.     Boolean            isColor;
  249.     
  250.     index=(**theData).windowIndex;
  251.     gBoundsRect[index]=gTheWindow[index]->portRect;
  252.     OffsetRect(&gBoundsRect[index], -gBoundsRect[index].left, -gBoundsRect[index].top);
  253.  
  254.     if (gHasColorQD)    /* w/o Color Quickdraw, GWorlds may not be supported */
  255.     {
  256.         if (gTheGWorld[index]==0L)        /* create new graphics world if none exists */
  257.         {
  258.             /* try to create new graphics world; display error if unsuccessful */
  259.             if (NewGWorld(&gTheGWorld[index],
  260.                 (GetBiggestDeviceDepth(theData)>=(**theData).maxDepth) ?
  261.                 (**theData).maxDepth : 0, &gBoundsRect[index], 0L, 0L, 0)!=0)
  262.             {
  263.                 HandleError(kNoMemory, FALSE);
  264.                 return;
  265.             }
  266.             (**theData).windowDepth=GetWindowDepth((WindowDataHandle)theData);
  267.             NoPurgePixels(GetGWorldPixMap(gTheGWorld[index]));    /* never purge our pixmap! */
  268.             updateResult=1;
  269.         }
  270.         else updateResult=0;
  271.         
  272.         GetGWorld(¤tGWorld, ¤tGDHandle);    /* get current settings */
  273.         LockPixels(GetGWorldPixMap(gTheGWorld[index]));    /* important!  copybits may move mem */
  274.         /* update offscreen graphics world, compensating for change in pixel depth */
  275.         updateResult|=(unsigned long)UpdateGWorld(&gTheGWorld[index],
  276.             (GetBiggestDeviceDepth(theData)>=(**theData).maxDepth) ? (**theData).maxDepth :
  277.             0, &gBoundsRect[index], 0L, 0L, 0);
  278.         SetGWorld(gTheGWorld[index], 0L);                /* set to our offscreen gworld */
  279.         
  280.         isColor=WindowIsColor(theData);
  281.         if (isColor!=(**theData).isColor)
  282.         {
  283.             (**theData).isColor=isColor;
  284.             updateResult=1;
  285.         }
  286.         
  287.         if ((updateResult!=0L) || ((**theData).windowDepth!=GetWindowDepth(theData)))
  288.         {
  289.             (**theData).windowDepth=GetWindowDepth((WindowDataHandle)theData);    /* save new depth */
  290.             (**theData).offscreenNeedsUpdate=TRUE;                /* we'll need to redraw */
  291.             ((**theData).dispatchProc)((WindowDataHandle)theData, kChangeDepth, 0L);
  292.         }
  293.     }
  294.     else    /* deal with (guaranteed) B/W bitmaps manually */
  295.     {
  296.         if (gBWGrafPtr[index]==0L)    /* create new offscreen bitmap if none exists */
  297.         {
  298.             gBWGrafPtr[index]=&gBWGrafPort[index];
  299.             OpenPort(gBWGrafPtr[index]);    /* make a new port */
  300.             
  301.             /* calculate the size of the offscreen bitmap from the boundsrect */
  302.             offRowBytes=(((gBoundsRect[index].right-gBoundsRect[index].left)+15)>>4)<<1;
  303.             sizeOfOff=(long)(gBoundsRect[index].bottom-gBoundsRect[index].top)*offRowBytes;
  304.             
  305.             gBWBitMap[index]=NewPtr(sizeOfOff);        /* allocate space for bitmap */
  306.             if (gBWBitMap[index]==0L)                /* abort if unsuccessful */
  307.             {
  308.                 ClosePort(gBWGrafPtr[index]);        /* cleaning up... */
  309.                 gBWGrafPtr[index]=0L;
  310.                 HandleError(kNoMemory, FALSE);        /* displaying error... */
  311.                 return;                                /* and aborting... */
  312.             }
  313.             
  314.             gBWGrafPort[index].portBits.baseAddr=gBWBitMap[index];    /* --> our bitmap */
  315.             gBWGrafPort[index].portBits.rowBytes=offRowBytes;        /* bitmap size */
  316.             gBWGrafPort[index].portBits.bounds=                        /* bitmap bounds */
  317.                 gBWGrafPort[index].portRect=gBoundsRect[index];
  318.             
  319.             SetPort(gBWGrafPtr[index]);
  320.             (**theData).offscreenNeedsUpdate=TRUE;
  321.         }
  322.         else SetPort(gBWGrafPtr[index]);            /* set port for subsequent drawing */
  323.     }    
  324.     
  325.     if ((**theData).offscreenNeedsUpdate)            /* if we need to redraw */
  326.     {
  327.         (**theData).offscreenNeedsUpdate=FALSE;        /* not anymore */
  328.         /* call window's dispatch and tell it to redraw itself */
  329.         ((**theData).dispatchProc)((WindowDataHandle)theData, kUpdate, GetWindowDepth((WindowDataHandle)theData));
  330.     }
  331.     
  332.     if (gHasColorQD)
  333.         SetGWorld(currentGWorld, currentGDHandle);    /* restore old settings */
  334.     
  335.     SetPort(gTheWindow[(**theData).windowIndex]);
  336.     
  337.     /* copy offscreen bitmap from graphics world or bitmap to onscreen window */
  338.     if (((**theData).dispatchProc)((WindowDataHandle)theData, kCopybits,
  339.         gHasColorQD ? (unsigned long)gTheGWorld[index] : (unsigned long)gBWGrafPtr[index])==kFailure)
  340.         CopyBits(gHasColorQD ? &(((GrafPtr)gTheGWorld[index])->portBits) :
  341.                     &(gBWGrafPtr[index]->portBits), &(gTheWindow[index]->portBits),
  342.                     &gBoundsRect[index], &gBoundsRect[index], 0, 0L);
  343.     
  344.     for (index=0; index<MAX_TE_HANDLES; index++)
  345.     {
  346.         if ((**theData).hTE[index]!=0L)
  347.             TEUpdate(&(gTheWindow[index]->portRect), (**theData).hTE[index]);
  348.     }
  349.  
  350.     if (gHasColorQD)
  351.         UnlockPixels(GetGWorldPixMap(gTheGWorld[index]));    /* remember we locked these? */
  352.     
  353.     ValidRect(&(gTheWindow[index]->portRect));        /* so we don't reupdate */
  354. }
  355.  
  356. Boolean CloseTheWindow(ExtendedWindowDataHandle theData)
  357. {
  358.     short            index;
  359.     
  360.     index=(**theData).windowIndex;
  361.     
  362.     /* if the window's dispatch cancels the close, abort */
  363.     if (((**theData).dispatchProc)((WindowDataHandle)theData, kClose, 0L)==kCancel)
  364.         return FALSE;
  365.     
  366.     DisposeWindow(gTheWindow[index]);    /* get rid of the actual window in memory */
  367.     gTheWindow[index]=0L;                /* so _we_ know the window doesn't exist */
  368.     KillOffscreen(index);                /* kill offscreen bitmaps left over */
  369.     
  370.     /* tell window's dispatch that it's disposed of now */
  371.     ((**theData).dispatchProc)((WindowDataHandle)theData, kDispose, 0L);
  372.     
  373.     return TRUE;    /* successful close */
  374. }
  375.  
  376. PicHandle DrawThePicture(PicHandle thePict, short whichPict, short x, short y)
  377. /* a standard routine for loading a picture (if necessary) and then drawing it */
  378. {
  379.     Rect            temp;
  380.     
  381.     if (thePict==0L)        /* get it if it doesn't exist */
  382.         thePict=(PicHandle)GetPicture(whichPict);
  383.     
  384.     HLock((Handle)thePict);        /* lock it down for dereferencing to get picture bounds */
  385.     temp.top=y;
  386.     temp.left=x;
  387.     temp.bottom=temp.top+(**thePict).picFrame.bottom-(**thePict).picFrame.top;
  388.     temp.right=temp.left+(**thePict).picFrame.right-(**thePict).picFrame.left;
  389.     DrawPicture(thePict, &temp);    /* draw picture at (x,y) */
  390.     HUnlock((Handle)thePict);        /* unlock for better memory management */
  391.     return thePict;
  392. }
  393.  
  394. PicHandle ReleaseThePict(PicHandle thePict)
  395. {
  396.     if (thePict!=0L)    /* if exists, release it */
  397.         ReleaseResource((Handle)thePict);
  398.     return 0L;
  399. }
  400.  
  401. Boolean SetPortToOffscreen(WindowDataHandle theData)
  402. {
  403.     short            index;
  404.     
  405.     index=(**theData).windowIndex;
  406.     
  407.     if (gHasColorQD)
  408.     {
  409.         if (gTheGWorld[index]==0L)
  410.             return FALSE;
  411.         GetGWorld(¤tGWorld, ¤tGDHandle);    /* get current settings */
  412.         LockPixels(GetGWorldPixMap(gTheGWorld[index]));    /* important!  copybits may move mem */
  413.         SetGWorld(gTheGWorld[index], 0L);
  414.     }
  415.     else
  416.     {
  417.         if (gBWGrafPtr[index]==0L)
  418.             return FALSE;
  419.         SetPort(gBWGrafPtr[index]);
  420.     }
  421.     
  422.     return TRUE;
  423. }
  424.  
  425. void RestorePortToScreen(WindowDataHandle theData)
  426. {
  427.     if (gHasColorQD)
  428.     {
  429.         SetGWorld(currentGWorld, currentGDHandle);    /* restore old settings */
  430.         UnlockPixels(GetGWorldPixMap(gTheGWorld[(**theData).windowIndex]));
  431.     }
  432.     
  433.     SetPort(gTheWindow[(**theData).windowIndex]);
  434. }
  435.  
  436. GrafPtr GetOffscreenGrafPtr(WindowDataHandle theData)
  437. {
  438.     if (gHasColorQD)
  439.         return (GrafPtr)gTheGWorld[(**theData).windowIndex];
  440.     else
  441.         return gBWGrafPtr[(**theData).windowIndex];
  442. }
  443.  
  444. GrafPtr GetWindowGrafPtr(WindowDataHandle theData)
  445. {
  446.     return (GrafPtr)gTheWindow[(**theData).windowIndex];
  447. }
  448.  
  449. void KillOffscreen(short index)
  450. {
  451.     if (gTheGWorld[index]!=0L)
  452.         DisposeGWorld(gTheGWorld[index]);
  453.     if (gBWGrafPtr[index]!=0L)
  454.         DisposePtr((Ptr)gBWGrafPtr[index]);
  455.     gTheGWorld[index]=0L;
  456.     gBWGrafPtr[index]=0L;
  457. }
  458.  
  459. void ShutDownTheGraphics(void)
  460. {
  461.     short            i;
  462.     
  463.     /* send shutdown messages to the shell's windows */
  464.     ((**(gTheWindowData[kAbout])).dispatchProc)((WindowDataHandle)gTheWindowData[kAbout], kShutdown, 0L);
  465.     ((**(gTheWindowData[kHelp])).dispatchProc)((WindowDataHandle)gTheWindowData[kHelp], kShutdown, 0L);
  466.     ((**(gTheWindowData[kAboutMSG])).dispatchProc)((WindowDataHandle)gTheWindowData[kAboutMSG], kShutdown, 0L);
  467.     
  468.     for (i=0; i<NUM_WINDOWS; i++)
  469.     {
  470.         KillOffscreen(i);
  471.         if (gTheWindowData[i]!=0L)
  472.             DisposeHandle((Handle)gTheWindowData[i]);
  473.         if (gTheWindow[i]!=0L)
  474.             DisposeWindow(gTheWindow[i]);
  475.     }
  476. }
  477.